#version 450

in vec4 fragPos;
in vec2 fragUv;
in flat vec2 fragTexIdx;
in flat uint fragAnimSize;
in vec3 fragNormal;
in float fragOcclusionLighting;
in vec3 fragLighting;


layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outPos;
layout(location = 2) out vec4 outNormal;
layout(location = 3) out vec4 outLight;


uniform sampler2D uTexture;
uniform float uFrame;

void main() {
    float texSize = textureSize(uTexture, 0).x;
    float texTileSize = texSize / 16.0;
    uint frame = uint(round(uFrame)) % fragAnimSize;

    vec2 animIdx = fragTexIdx + vec2(frame, 0);
    animIdx.y += float(int(animIdx.x) / int(texTileSize));
    animIdx.x = float(int(animIdx.x) % int(texTileSize));

    vec2 uvIdx = fragUv + animIdx;
    vec2 uv = uvIdx * 16.0 / textureSize(uTexture, 0);
    vec4 color = texture(uTexture, uv);
    if (color.a < 1) discard; // only keep opaque objects
    outColor = vec4(color.xyz, color.a);
    outPos = fragPos;
    outNormal = vec4(normalize(gl_FrontFacing ? fragNormal : -fragNormal), 1.0f);
    outLight = vec4(fragLighting, 1.0f);
}